﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

/*
 * 材料处置暂时用了，下面为后台配置tpabma21
 * 
19	K	出入库操作	30	KA	材料处置	37	/page/user/ym05cz/ym05cz?PRO_TYPE=MAT_NO	ico_06_1a.png	LTNM	 	 	 	 	 
20	K	出入库操作	30	KB	散状材料处置	38	/page/user/ym05cz/ym05cz?PRO_TYPE=BILL_OF_LADING_NO	ico_06_1a.png	LTNM	 	 	 	 	 
24	Y	出入库记录	40	KC	处置出库记录	40	/page/user/ym00inq/ym00inq?FORMNAME=CZ	ico_07_1a.png	LTNM	 	 	 	 	 

 * 
 */
namespace DingApi.Controllers
{
    [RoutePrefix("api/Ym05cz")]
    public class Ym05czController : ApiController
    {

        /// <summary>
        /// /系统材料好；
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PostYm05cz_Bill_inq")]
        public System.Data.DataSet PostYm05cz_Bill_inq([FromBody]  JObject obj)
        {
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");


            string MAT_NO = obj["MAT_NO"] == null ? "" : obj["MAT_NO"].ToString();
            string BILL_OF_LADING_NO = obj["BILL_OF_LADING_NO"] == null ? "" : obj["BILL_OF_LADING_NO"].ToString();

            string PRO_TYPE = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString();




            string sql_temp = "";
          
            EI.EIInfo Block_TMM0001 = new EI.EIInfo();

             

            //根据【标签.批次号 + 提单子项.规格 物料主档表找材料及材料是否存在】
            //物料主档：PROD_CODE,SG_SIGN,MAT_SPEC_DESC,PACK_MODE_CODE + STOCK_NO + PROD_LOT_NO 基本唯一了
            sql_temp = " SELECT  ' ' PACK_MODE_NAME, t.*   FROM TMM0001 t "
                        + " WHERE  MAT_NO ='" + BILL_OF_LADING_NO + "'"
                        ;

            Block_TMM0001 = EF.Utility.ExecQuery(sql_temp);
            if (Block_TMM0001.Tables[0].Rows.Count < 1)
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + BILL_OF_LADING_NO + "]不存在！");
                return outBlock;
            }

            Block_TMM0001.Tables[0].TableName = "TMM0001";
            outBlock.Tables.Add(Block_TMM0001.Tables[0].Copy());
            


            outBlock.Tables["SYS"].Rows.Add(0, "");
            return outBlock;


        }


        

        /// <summary>
        /// 处置出库材料号扫描；
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PostYm05cz_mat_no_inq")]
        public System.Data.DataSet PostYm05cz_mat_no_inq([FromBody]  JObject obj)
        {
            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            

            string MAT_NO   = obj["MAT_NO"] == null ? "" : obj["MAT_NO"].ToString();
            string BILL_OF_LADING_NO = obj["BILL_OF_LADING_NO"] == null ? "" : obj["BILL_OF_LADING_NO"].ToString();

            string PRO_TYPE = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString();

 


            string sql_temp = ""; 
            EI.EIInfo Block_TPMMM02N = new EI.EIInfo(); 
            EI.EIInfo Block_TMM0001 = new EI.EIInfo();



           


            if (PRO_TYPE == "MAT_NO")
            {
                string sql_str0 = " select LOCATION_NO,MAT_NO, mat_wt,PROD_CODE,PROD_LOT_NO,qual_grade_code,Pack_Mode_Name ";
                sql_str0 += "       ,pack_mode_code,stock_no,mat_spec_desc";
                sql_str0 += "       ,SG_STD,SG_SIGN , ' ' BILL_OF_LADING_NO,ARCHIVE_FLAG  ";
                sql_str0 += "      ,STATUS  from TPMMM02N                                                                                  ";
                sql_str0 += "       where mat_no = '" + MAT_NO + "' ";

                Block_TPMMM02N = EF.Utility.ExecQuery(sql_str0);
                if (Block_TPMMM02N.Tables[0].Rows.Count < 1)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "标签材料[" + MAT_NO + "]不存在！");
                    return outBlock;
                }
                 

                //根据【标签.批次号 + 提单子项.规格 物料主档表找材料及材料是否存在】
                //物料主档：PROD_CODE,SG_SIGN,MAT_SPEC_DESC,PACK_MODE_CODE + STOCK_NO + PROD_LOT_NO 基本唯一了
                sql_temp = " SELECT  t.*   FROM TMM0001 t "
                            + " WHERE STOCK_NO= '" + Block_TPMMM02N.Tables[0].Rows[0]["STOCK_NO"].ToString() + "'  "
                            + " AND LOT_NO ='" + Block_TPMMM02N.Tables[0].Rows[0]["PROD_LOT_NO"].ToString() + "'"
                            + " AND MAT_NUM  >=1  AND MAT_WT >0 "
                            + " AND rownum = 1  "
                            + " AND PROD_CODE ='" + Block_TPMMM02N.Tables[0].Rows[0]["PROD_CODE"].ToString() + "'"
                            + " AND SG_SIGN ='" + Block_TPMMM02N.Tables[0].Rows[0]["SG_SIGN"].ToString() + "'"
                            + " AND MAT_SPEC_DESC ='" + Block_TPMMM02N.Tables[0].Rows[0]["MAT_SPEC_DESC"].ToString().Replace("M", "").Replace("m", "") + "'"
                            + " AND PACK_MODE_CODE ='" + Block_TPMMM02N.Tables[0].Rows[0]["PACK_MODE_CODE"].ToString() + "'"
                            ;

                Block_TMM0001 = EF.Utility.ExecQuery(sql_temp);
                if (Block_TMM0001.Tables[0].Rows.Count < 1)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "物料主档找不到到满足签材料[" + MAT_NO + "]的材料！");
                    return outBlock;
                }
                else
                {
                    Block_TPMMM02N.Tables[0].Rows[0]["BILL_OF_LADING_NO"] = Block_TMM0001.Tables[0].Rows[0]["MAT_NO"];
                }

                Block_TPMMM02N.Tables[0].TableName = "TMM0001";
                outBlock.Tables.Add(Block_TPMMM02N.Tables[0].Copy());


            }
            else if (PRO_TYPE == "BILL_OF_LADING_NO") //无唯一标签的材料好】
            {

                //根据【标签.批次号 + 提单子项.规格 物料主档表找材料及材料是否存在】
                //物料主档：PROD_CODE,SG_SIGN,MAT_SPEC_DESC,PACK_MODE_CODE + STOCK_NO + PROD_LOT_NO 基本唯一了
                sql_temp = " SELECT  t.*   FROM TMM0001 t "
                            + " WHERE  MAT_NO ='" + BILL_OF_LADING_NO + "'"
                            ; 

                Block_TMM0001 = EF.Utility.ExecQuery(sql_temp);
                if (Block_TMM0001.Tables[0].Rows.Count < 1)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + BILL_OF_LADING_NO + "]不存在！");
                    return outBlock;
                }

                Block_TMM0001.Tables[0].TableName = "TMM0001";
                outBlock.Tables.Add(Block_TMM0001.Tables[0].Copy());

            }
             

         
            

            outBlock.Tables["SYS"].Rows.Add(0, "");
            return outBlock;

        }



        /// <summary>
        /// 处置入库材料扫描
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PostYm05cz_mat_no2")]
        public System.Data.DataSet PostYm05cz_mat_no2([FromBody]  JObject obj)
        {
            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");


            string MAT_NO = obj["MAT_NO"] == null ? "" : obj["MAT_NO"].ToString();
            string BILL_OF_LADING_NO = obj["BILL_OF_LADING_NO"] == null ? "" : obj["BILL_OF_LADING_NO"].ToString();

            string PRO_TYPE = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString();




            string sql_temp = "";
            EI.EIInfo Block_TPMMM02N = new EI.EIInfo();
            EI.EIInfo Block_TMM0001 = new EI.EIInfo();

             

            if (MAT_NO!="")
            {
                string sql_str0 = " select  t.* , ' ' BILL_OF_LADING_NO ";
                sql_str0 += "        from TPMMM02N   t                                                                                 ";
                sql_str0 += "       where mat_no = '" + MAT_NO + "' ";

                Block_TPMMM02N = EF.Utility.ExecQuery(sql_str0);
                if (Block_TPMMM02N.Tables[0].Rows.Count < 1)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "标签材料[" + MAT_NO + "]不存在！");
                    return outBlock;
                }

                if (Block_TPMMM02N.Tables[0].Rows[0]["STATUS"].ToString() !="11")
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "标签材料[" 
                        + MAT_NO + "]状态["
                        + Block_TPMMM02N.Tables[0].Rows[0]["STATUS"].ToString() 
                        + "]不对！");
                    return outBlock;
                }


                Block_TPMMM02N.Tables[0].TableName = "TMM0001";
                outBlock.Tables.Add(Block_TPMMM02N.Tables[0].Copy());

            }
            else  //mat_no 为空
            {
                if (BILL_OF_LADING_NO != "")
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "系统材料[" + BILL_OF_LADING_NO + "]不可为空！");
                    return outBlock;
                }

                sql_temp = " SELECT  t.*   FROM TMM0001 t "
                            + " WHERE  MAT_NO ='" + BILL_OF_LADING_NO + "'"
                            ;

                Block_TMM0001 = EF.Utility.ExecQuery(sql_temp);
                if (Block_TMM0001.Tables[0].Rows.Count < 1)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + BILL_OF_LADING_NO + "]不存在！");
                    return outBlock;
                }

                //包装方式
                Block_TMM0001.Tables[0].Rows[0]["PACK_MODE_CODE"] = "S04";
              


                Block_TMM0001.Tables[0].TableName = "TMM0001";
                outBlock.Tables.Add(Block_TMM0001.Tables[0].Copy());

            }





            outBlock.Tables["SYS"].Rows.Add(0, "");
            return outBlock;

        }

        

        //画面初始化【主要是各类小代码，类似之前画面的初始化参数】，暂时未用；
        //[HttpPost]
        //[Route("PostYm05cz_Load")]
        //public System.Data.DataSet PostYm05cz_Load([FromBody]  JObject obj)
        //{
        //    //系统信息；
        //    EI.EIInfo outBlock = new EI.EIInfo();
        //    outBlock.Tables.Add("SYS");
        //    outBlock.Tables["SYS"].Columns.Add("FLAG");
        //    outBlock.Tables["SYS"].Columns.Add("MSG");


        //    outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
        //    return outBlock;


        //}


        //处置出库保存
        [HttpPost]
        [Route("PostYm05cz_save")]
        public System.Data.DataSet PostYm05cz_save([FromBody]  JObject obj)
        {

            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");

            

            // MAT_NO，PROD_LOT_NO 必须有一个值；钉钉前端校验；
            string BILL_OF_LADING_NO = obj["BILL_OF_LADING_NO"] == null ? "" : obj["BILL_OF_LADING_NO"].ToString();
            string STOCK_NO = obj["STOCK_NO"] == null ? "" : obj["STOCK_NO"].ToString();


            string QUAL_GRADE_CODE = obj["QUAL_GRADE_CODE"] == null ? " " : obj["QUAL_GRADE_CODE"].ToString();
         
            

            string OUT_STOCK_NO = obj["OUT_STOCK_NO"] == null ? "" : obj["OUT_STOCK_NO"].ToString();


            string USERID = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string USERNAME = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();
            string PRO_TYPE = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString();


            DataTable table_rec_out = JsonConvert.DeserializeObject<DataTable>(obj["table_rec_out"].ToString(), new DingApi.ReadAheadDataTableConverter());
            DataTable table_rec_in =  JsonConvert.DeserializeObject<DataTable>(obj["table_rec_in"].ToString(), new DingApi.ReadAheadDataTableConverter());

         


           string sql_temp = " SELECT  t.*   FROM TMM0001 t "
                          + " WHERE  MAT_NO ='" + BILL_OF_LADING_NO + "'"
                          ;

            EI.EIInfo Block_TMM0001 = EF.Utility.ExecQuery(sql_temp);
            if (Block_TMM0001.Tables[0].Rows.Count < 1)
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + BILL_OF_LADING_NO + "]不存在！");
                return outBlock;
            }

            double mat_wt = 0, mat_num = 0;
            for (int i = 0; i < table_rec_out.Rows.Count; i++)
            {
                mat_num = mat_num + 1;
                mat_wt = mat_wt + double.Parse(table_rec_out.Rows[i]["MAT_WT"].ToString());
            }


            Block_TMM0001.Tables[0].Rows[0]["MAT_NUM"] = mat_num;
            Block_TMM0001.Tables[0].Rows[0]["MAT_WT"] = mat_wt;


            for (int i = 0; i < table_rec_in.Rows.Count; i++)
            {
                table_rec_in.Rows[i]["QUAL_GRADE_CODE"] = QUAL_GRADE_CODE;
            }


         



            EI.EIInfo inBlock = new EI.EIInfo();
             
            //处置出库，合并成一条记录；
            inBlock.Tables[0].Merge(Block_TMM0001.Tables[0]); //处置出库；

            inBlock.Tables.Add();
            inBlock.Tables.Add();
            inBlock.Tables.Add();

             
            //多条记录；
            inBlock.Tables[1].Merge(table_rec_in);//处置入库

             
           
            //
            inBlock.Tables[2].Columns.Add("prod_type");
            inBlock.Tables[2].Columns.Add("iostock_date");
            inBlock.Tables[2].Rows.Add(0);
            inBlock.Tables[2].Rows[0]["prod_type"] = "";
            inBlock.Tables[2].Rows[0]["iostock_date"] = DateTime.Now.ToString("yyyyMMddHHmmss");


            //出库标签【未合并的】
            inBlock.Tables[3].Merge(table_rec_out);//标签跟踪；



            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.company_name = "常熟市龙腾特种钢有限公司";

            inBlock.sys_info.userid = USERID;
            inBlock.sys_info.username = USERNAME;
            inBlock.sys_info.formname = FORMNAME;






            // 调用后台services保存信息
            EI.EIInfo  outBlock2 = EI.EITuxedo.CallService("pmqmtj00_ins", inBlock);
            if (outBlock2.sys_info.flag != 0)
            {
                outBlock.Tables["SYS"].Rows.Add(outBlock2.sys_info.flag, outBlock2.sys_info.msg);
                return outBlock;
            }
            else
            {
                outBlock2.Tables[0].TableName = "Table0";
                outBlock.Tables.Add(outBlock2.Tables[0].Copy()); 
                outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, "材料处置出库保存成功[" + outBlock2.DataSet.Tables[0].Rows[0]["IOSTOCK_BILL_NO"].ToString() + "]");
                return outBlock;
            }


        }


        //销售出库确认PA31(19->31);确认回退PB31（31->19);DEL
        [HttpPost]
        [Route("PostYm05cz_pro")]
        public System.Data.DataSet PostYm05cz_pro([FromBody]  JObject obj)
        {

            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");




            string out_stock_no = obj["OUT_STOCK_NO"]   == null ? ""    : obj["OUT_STOCK_NO"].ToString();
            string pro_type     = obj["PRO_TYPE"]       == null ? " "   : obj["PRO_TYPE"].ToString();






            string USERID   = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string USERNAME = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();




            EI.EIInfo inblock = new EI.EIInfo();


            inblock.Tables[0].Columns.Add("IOSTOCK_BILL_NO");
            inblock.Tables[0].Columns.Add("OUT_STOCK_NO");

            inblock.Tables[0].Rows.Add(out_stock_no, out_stock_no);





            inblock.sys_info.company_code = "LT";
            inblock.sys_info.company_name = "常熟市龙腾特种钢有限公司";

            inblock.sys_info.userid = USERID;
            inblock.sys_info.username = USERNAME;
            inblock.sys_info.formname = FORMNAME;


            //
            inblock.Tables[0].TableName = pro_type;

            string svr = "pmmm02n_pro";

            if (pro_type.ToUpper() == "DEL") //
            {
                svr = "pmqmtj00_del";
            }


            // 调用后台services保存信息 //调拨出库；
            EI.EIInfo outBlock2 = EI.EITuxedo.CallService(svr, inblock);
            if (outBlock2.sys_info.flag != 0)
            {
                outBlock.Tables["SYS"].Rows.Add(outBlock2.sys_info.flag, outBlock2.sys_info.msg);
                return outBlock;
            }
            else
            {
                outBlock.Tables["SYS"].Rows.Add(outBlock2.sys_info.flag, outBlock2.sys_info.msg);
                return outBlock;
            }


        }





        
 

    }
}